<?php

/**
 * Reservacion form.
 *
 * @package    blackDrums
 * @subpackage form
 * @author     your name here
 */
class ReservacionForm extends BaseReservacionForm
{
  public function configure()
  {
     
      if(!$this->isNew()){
          unset($this['status']);
         // $this->widgetSchema['status']->setHidden(true);
//        $this->widgetSchema['status']->setOption('is_hidden', true);
//        $this->widgetSchema['status']->setOption('type', 'hidden');
        //$this->widgetSchema['status'] = new sfWidgetFormInputHidden();
      }
      $this->widgetSchema->setLabels(array(
        'idSala'    => 'Sala',
        'idClienteNatural' => 'Cliente',
        'status' => 'Status',
        'horaInicio' => 'Hora Inicio',
        'horaFin' => 'Hora Fin',
        'idBanda' => 'Banda'
       ));

      $this->validatorSchema->setPostValidator(
        new sfValidatorCallback(array('callback' => array($this, 'validarNegocio')))
      );
      
      if($this->isNew()){
           $this->setValidator('status', new sfValidatorFile(array(
             'required' => false,
           )));
      }

        $this->widgetSchema->setLabels(array(
                'idSala'    => 'Sala',
                'idClienteNatural' => 'Cliente',
                'status' => 'Status',
                'horaInicio' => 'Hora Inicio',
                'horaFin' => 'Hora Fin',
                'idBanda' => 'Banda'
        ));

        $this->validatorSchema->setPostValidator(
                new sfValidatorCallback(array('callback' => array($this, 'validarNegocio')))
        );

        if($this->isNew()) {
            $this->setValidator('status', new sfValidatorFile(array(
                            'required' => false,
            )));
        }

        $this->widgetSchema['fecha']= new sfWidgetFormDateJQueryUI(array("change_month" => true, "change_year" => true));
    }

    public function validarNegocio($validator, $values) {
        $horaIni = $values['horaInicio'];
        $horaFin = $values['horaFin'];
        $dia = $values['fecha'];
        $sala = $values['idSala'];
        $idReserva = $values['idReservacion'];

        if(ReservacionPeer::reservacionesDelDia($horaIni, $horaFin, $dia, $sala,$idReserva)==
                'No disponible') {
            throw new sfValidatorError($validator, 'La sala no esta disponible a
             esta hora');
        }

        $cedula = $values['idClienteNatural'];

        if(Reservacion::estadoReservacion($cedula)=='moroso') {
            throw new sfValidatorError($validator, 'Cliente moroso');

        }

        $idBanda = $values['idBanda'];

        if(Reservacion::estadoReservacionBanda($idBanda)=='moroso') {
            throw new sfValidatorError($validator, 'Banda morosa');
        }

        return $values;
    }


}
